package.cpath = "C:\\Users\\destroy314\\Documents\\Gitee\\IGCA\\2048\\?.dll"
Map = require("lib.dll.api")

function love.load()
    Map.init()
    Map.gene()
    Win = love.graphics.newImage("win.png")
    Radius = 100
    Border_h = Radius * 3 / 2 + 8
    Border_w = Radius * 2 * math.pow(1, 1 / 3) - 16
    Midx = love.graphics.getWidth() / 2
    Midy = love.graphics.getHeight() / 2
    Pos0_x = Midx - Border_w * 2
    Pos0_y = Midy - Border_h * 2
    Oritx = 0
    Ority = 0
    Touching = 0
    P = 0
    Time = 0
    Status = 0
    Font = love.graphics.newFont("Comic_Sans.ttf", 50)
    Score = 0
end

Switch = {
    [1] = function()
        Drawarrow(-math.pi / 3)
    end,
    [2] = function()
        Drawarrow(0)
    end,
    [3] = function()
        Drawarrow(math.pi / 3)
    end,
    [4] = function()
        Drawarrow(2 * math.pi / 3)
    end,
    [5] = function()
        Drawarrow(math.pi)
    end,
    [6] = function()
        Drawarrow(-2 * math.pi / 3)
    end
}

function love.draw()
    if Status == 1 then
        love.graphics.setColor(1, 1, 1)
        love.graphics.draw(Win, 0, 0, 0, 0.5, 0.5)
        return
    end
    love.graphics.setBackgroundColor(250 / 255, 227 / 255, 217 / 255)
    for i = 0, 4 do
        for j = 0, 4 do
            local dx = 46 + math.pow(-1, i + 1) * 46
            if i == 0 then
                dx = dx + Border_w
            elseif i == 3 then
                dx = dx - Border_w
            elseif i == 4 then
                dx = dx - Border_w
            end

            if
                not ((i == 0 and j > 2) or (i == 1 and j > 3) or
                    (i == 3 and j < 1) or
                    (i == 4 and j < 2))
             then
                local num = Map.num(i, j)
                local t = math.log(num) / math.log(2)
                if num == 0 then
                    Drawcell(
                        Pos0_x + dx + j * Border_w,
                        Pos0_y + i * Border_h,
                        176 / 255,
                        196 / 255,
                        222 / 255
                    )
                else
                    Drawcell(
                        Pos0_x + dx + j * Border_w,
                        Pos0_y + i * Border_h,
                        (10 - t) / 10,
                        (10 - t) / 10,
                        (10 - t) / 10
                    )
                end
                Write(Pos0_x + dx + j * Border_w, Pos0_y + i * Border_h, num)
                if Time ~= 0 and Map.shi(i, j) == 1 then
                    Drawcell(
                        Pos0_x + dx + j * Border_w,
                        Pos0_y + i * Border_h,
                        1,
                        1,
                        1
                    )
                end
            end
        end
    end
    local touches = love.touch.getTouches()
    for i, id in ipairs(touches) do
        local x, y = love.touch.getPosition(id)
        local angle = math.atan((Ority - y) / (x - Oritx))
        if x < Oritx then
            angle = angle + math.pi
        end
        if math.pi / 6 < angle and math.pi / 2 > angle then
            P = 1
        elseif -1 * math.pi / 6 < angle and 1 * math.pi / 6 > angle then
            P = 2
        elseif -1 * math.pi / 2 < angle and -1 * math.pi / 6 > angle then
            P = 3
        elseif 7 * math.pi / 6 < angle and 3 * math.pi / 2 > angle then
            P = 4
        elseif 5 * math.pi / 6 < angle and 7 * math.pi / 6 > angle then
            P = 5
        elseif math.pi / 2 < angle and 5 * math.pi / 6 > angle then
            P = 6
        end
        if
            (math.pow(math.pow(x - Oritx, 2) + math.pow(y - Ority, 2), 1 / 2) <=
                100)
         then
            love.graphics.setColor(1, 0, 0, 0.5)
            love.graphics.circle("fill", Oritx, Ority, 100)
            P = 0
        else
            love.graphics.line(Oritx, Ority, x, y)
            local f = Switch[P]
            if f then
                f()
            end
        end
    end
    love.graphics.setColor(0, 0, 0)
    local s = love.graphics.newText(Font, "Score:")
    local n = love.graphics.newText(Font, Score)
    love.graphics.draw(s, 0, 0)
    love.graphics.draw(n, 160, 0)
end

function Drawcell(px, py, r, g, b)
    love.graphics.translate(px, py)
    love.graphics.rotate(math.pi / 6)
    love.graphics.setColor(255 / 255, 182 / 255, 185 / 255)
    love.graphics.circle("fill", 0, 0, Radius + 8, 6)
    love.graphics.setColor(r, g, b)
    love.graphics.circle("fill", 0, 0, Radius, 6)
    love.graphics.rotate(-math.pi / 6)
    love.graphics.translate(-px, -py)
end

function Write(px, py, n)
    if n >= 128 then
        love.graphics.setColor(1, 1, 1)
    else
        love.graphics.setColor(0, 0, 0)
    end

    local plainText = love.graphics.newText(Font, n)
    love.graphics.draw(plainText, px - 60, py - 40)
end

function love.touchpressed(id, x, y, dx, dy, pressure)
    Oritx = x
    Ority = y
end

function love.touchreleased(id, x, y, dx, dy, pressure)
    if Status == 1 then
        return
    end
    if P ~= 0 then
        Time = 0.1
        local changed
        changed, Status = Map.slip(P)
        if changed > 0 then
            Map.gene()
            Score = Score + changed
        end
    end
end

function Drawarrow(ang)
    love.graphics.setColor(0.5, 0.5, 0.5, 0.5)
    love.graphics.translate(Midx, Midy)
    love.graphics.rotate(ang)
    love.graphics.polygon("fill", -300, -60, 150, -60, 150, 60, -300, 60)
    love.graphics.polygon("fill", 150, -130, 300, 0, 150, 130)
    love.graphics.rotate(-ang)
    love.graphics.translate(-Midx, -Midy)
end

function love.update(dt)
    if Time ~= 0 then
        Time = Time - dt
    end
    if Time < 0 then
        Time = 0
    end
end
